package com.yf.ccdugtfood.ui.me;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.yf.ccdugtfood.activity.OrderAllActivity;
import com.yf.ccdugtfood.activity.AddressActivity;
import com.yf.ccdugtfood.activity.LoginActivity;
import com.yf.ccdugtfood.activity.MainActivity;
import com.yf.ccdugtfood.activity.MyCommentActivity;
import com.yf.ccdugtfood.R;
import com.yf.ccdugtfood.activity.WaitCommentActivity;
import com.yf.ccdugtfood.robot.RobotActivity;

import de.hdodenhof.circleimageview.CircleImageView;

import static android.content.Context.MODE_PRIVATE;

public class MeFragment extends Fragment {
    private View view;
    private TextView loginRegister;
    private TextView userName;
    private CircleImageView logoIcon;
    private MainActivity activity;
    private LinearLayout my_address;
    private LinearLayout my_robot;
    private LinearLayout my_comment;
    private LinearLayout about_us;
    private LinearLayout exit_login;
    private LinearLayout my_all_order;
    private LinearLayout my_wait_comment;
    private Context mContext;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_me,container,false);
        mContext = getActivity();
        activity = (MainActivity) getActivity();
        initLoginRegister(view);
        initAddress(view);
        initRobot(view);
        initComment(view);
        initUs(view);
        exitLogin(view);
        initAllOrder(view);
        initWaitComment(view);
        return view;
    }

    private void initWaitComment(View view) {
        my_wait_comment = view.findViewById(R.id.my_wait_comment);
        my_wait_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {SharedPreferences sp = getContext().getSharedPreferences("data",MODE_PRIVATE);
                String user_id = sp.getString("id",null);
                if (user_id!=null){
                    Intent intent = new Intent(activity, WaitCommentActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(mContext,"请先登录！",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initAllOrder(View view) {
        my_all_order = view.findViewById(R.id.my_all_order);
        my_all_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {SharedPreferences sp = getContext().getSharedPreferences("data",MODE_PRIVATE);
                String user_id = sp.getString("id",null);
                if (user_id!=null){
                    Intent intent = new Intent(activity, OrderAllActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(mContext,"请先登录！",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void exitLogin(View view) {
        exit_login = view.findViewById(R.id.exit_login);
        exit_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getContext().getSharedPreferences("data",MODE_PRIVATE);
                String user_id = sp.getString("id",null);
                if(user_id!=null){
                    new AlertDialog.Builder(activity).setTitle("退出登录")
                            .setMessage("确定要退出登录吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    SharedPreferences sp = getContext().getSharedPreferences("data", MODE_PRIVATE);
                                    sp.edit().clear().commit();
                                    userName.setText("昵称");
                                    loginRegister.setText("登录/注册>");
                                    loginRegister.setEnabled(true);
                                    Intent intent = new Intent(activity,LoginActivity.class);
                                    startActivity(intent);
                                }
                            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).show();
                }else{
                    Toast.makeText(mContext,"你还没登录，请先登录！",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initUs(View view) {
        about_us = view.findViewById(R.id.about_us);
        about_us.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(activity,"正在努力上线中...",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initComment(View view) {
        my_comment = view.findViewById(R.id.my_comment);
        my_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {SharedPreferences sp = getContext().getSharedPreferences("data",MODE_PRIVATE);
                String user_id = sp.getString("id",null);
                if (user_id!=null){
                    Intent intent = new Intent(activity, MyCommentActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(mContext,"请先登录！",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initRobot(View view) {
        my_robot = view.findViewById(R.id.my_robot);
        my_robot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, RobotActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initAddress(View view) {
        my_address = view.findViewById(R.id.my_address);
        my_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getContext().getSharedPreferences("data",MODE_PRIVATE);
                String user_id = sp.getString("id",null);
                if (user_id!=null){
                    Intent intent = new Intent(activity, AddressActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(mContext,"请先登录！",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initLoginRegister(View view) {
        loginRegister = (TextView) view.findViewById(R.id.loginRegister);
        userName = (TextView) view.findViewById(R.id.username);
        SharedPreferences sp = mContext.getSharedPreferences("data", MODE_PRIVATE);
        String user_name = sp.getString("user_name",null);
        String user_phone = sp.getString("user_phone",null);
        if (user_name !=null && user_phone!=null){
            userName.setText(user_name);
            loginRegister.setText(user_phone);
            loginRegister.setEnabled(false);
        }
        logoIcon = (CircleImageView) view.findViewById(R.id.logo_icon);
        loginRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity,LoginActivity.class);
                startActivity(intent);
            }
        });
    }
}
